/*     */ package com.zimbra.cs.db;
/*     */ 
/*     */ import java.io.BufferedReader;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.PrintStream;
/*     */ import java.io.PrintWriter;
/*     */ import java.sql.Connection;
/*     */ import java.sql.DriverManager;
/*     */ import java.sql.ResultSet;
/*     */ import java.sql.ResultSetMetaData;
/*     */ import java.sql.SQLException;
/*     */ import java.sql.Statement;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ import org.apache.commons.cli.CommandLine;
/*     */ import org.apache.commons.cli.GnuParser;
/*     */ import org.apache.commons.cli.HelpFormatter;
/*     */ import org.apache.commons.cli.Option;
/*     */ import org.apache.commons.cli.Options;
/*     */ import org.apache.commons.cli.ParseException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class JdbcClient
/*     */ {
/*     */   private static final String OPT_HELP = "h";
/*     */   private static final String OPT_USER = "u";
/*     */   private static final String OPT_PASSWORD = "p";
/*     */   private static final String OPT_JDBC_URL = "j";
/*     */   private static final String OPT_DRIVER_CLASS = "c";
/*     */   private static final String OPT_BATCH = "B";
/*     */   private static final String OPT_SKIP_COLUMN_NAMES = "N";
/*     */   private static final String NULL = "NULL";
/*  61 */   private static final Pattern PAT_QUIT = Pattern.compile("\\s*quit;?", 2);
/*  62 */   private static final Pattern PAT_EXIT = Pattern.compile("\\s*exit;?", 2);
/*  63 */   private static final Pattern PAT_SELECT = Pattern.compile("^\\s*SELECT", 2);
/*     */   
/*  65 */   private static final Pattern PAT_SEMICOLON = Pattern.compile("([^;]*);(.*)");
/*     */   
/*  67 */   private Options mOptions = new Options();
/*     */   private String mDriverClass;
/*     */   private String mUser;
/*     */   private String mPassword;
/*     */   private String mJdbcUrl;
/*  72 */   private boolean mBatch = false;
/*  73 */   private boolean mShowColumnNames = true;
/*     */   
/*     */   private JdbcClient(String[] args) {
/*  76 */     Option opt = new Option("h", "help", false, "Display this help message.");
/*  77 */     this.mOptions.addOption(opt);
/*     */     
/*  79 */     opt = new Option("c", "driver-class", true, "JDBC driver class name.");
/*  80 */     opt.setRequired(true);
/*  81 */     this.mOptions.addOption(opt);
/*     */     
/*  83 */     opt = new Option("u", "user", true, "User name.");
/*  84 */     opt.setRequired(true);
/*  85 */     this.mOptions.addOption(opt);
/*     */     
/*  87 */     opt = new Option("p", "password", true, "Password.");
/*  88 */     opt.setRequired(true);
/*  89 */     this.mOptions.addOption(opt);
/*     */     
/*  91 */     opt = new Option("j", "jdbc-url", true, "JDBC URL used for connecting to the database server.");
/*  92 */     opt.setRequired(true);
/*  93 */     this.mOptions.addOption(opt);
/*     */     
/*  95 */     this.mOptions.addOption("B", "batch", false, "Prints results without formatting, separated by tabs.");
/*  96 */     this.mOptions.addOption("N", "skip-column-names", false, "Don't write column names in results.");
/*     */     
/*  98 */     CommandLine cl = null;
/*     */     try
/*     */     {
/* 101 */       GnuParser parser = new GnuParser();
/* 102 */       cl = parser.parse(this.mOptions, args);
/*     */     } catch (ParseException e) {
/* 104 */       usage(e);
/* 105 */       System.exit(1);
/*     */     }
/*     */     
/* 108 */     if (cl.hasOption("h")) {
/* 109 */       usage(null);
/* 110 */       System.exit(0);
/*     */     }
/*     */     
/* 113 */     this.mDriverClass = cl.getOptionValue("c");
/* 114 */     this.mUser = cl.getOptionValue("u");
/* 115 */     this.mPassword = cl.getOptionValue("p");
/* 116 */     this.mJdbcUrl = cl.getOptionValue("j");
/*     */     
/* 118 */     if (cl.hasOption("B")) {
/* 119 */       this.mBatch = true;
/*     */     }
/* 121 */     if (cl.hasOption("N")) {
/* 122 */       this.mShowColumnNames = false;
/*     */     }
/*     */   }
/*     */   
/*     */   private void run()
/*     */   {
/*     */     try {
/* 129 */       Class.forName(this.mDriverClass);
/*     */     } catch (Throwable t) {
/* 131 */       System.err.println("Unable to load driver '" + this.mDriverClass + "': " + t);
/* 132 */       System.exit(1);
/*     */     }
/*     */     
/*     */ 
/* 136 */     Connection conn = null;
/*     */     try {
/* 138 */       conn = DriverManager.getConnection(this.mJdbcUrl, this.mUser, this.mPassword);
/*     */     } catch (SQLException e) {
/* 140 */       System.err.println("Unable to connect to " + this.mJdbcUrl + " using " + this.mDriverClass + ": " + e);
/* 141 */       System.exit(1);
/*     */     }
/*     */     
/*     */     try
/*     */     {
/* 146 */       BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
/* 147 */       String line = "";
/* 148 */       StringBuilder b = new StringBuilder();
/* 149 */       while (line != null)
/*     */       {
/* 151 */         Matcher quitMatcher = PAT_QUIT.matcher(line);
/* 152 */         Matcher exitMatcher = PAT_EXIT.matcher(line);
/* 153 */         if ((quitMatcher.matches()) || (exitMatcher.matches())) {
/*     */           break;
/*     */         }
/*     */         
/* 157 */         line = DbUtil.removeComments(line);
/* 158 */         Matcher semicolonMatcher = PAT_SEMICOLON.matcher(line);
/* 159 */         if (semicolonMatcher.matches())
/*     */         {
/* 161 */           b.append(semicolonMatcher.group(1));
/* 162 */           runSql(conn, b.toString());
/* 163 */           b = new StringBuilder();
/* 164 */           line = semicolonMatcher.group(2);
/*     */         } else {
/* 166 */           b.append(line);
/* 167 */           line = reader.readLine();
/*     */         }
/*     */       }
/*     */       
/*     */ 
/* 172 */       if (b.length() > 0) {
/* 173 */         runSql(conn, b.toString());
/*     */       }
/*     */     }
/*     */     catch (IOException e) {
/* 177 */       System.err.println(e);
/* 178 */       System.exit(1);
/*     */     }
/*     */     try
/*     */     {
/* 182 */       conn.close();
/*     */     } catch (SQLException e) {
/* 184 */       System.err.println(e);
/* 185 */       System.exit(1);
/*     */     }
/*     */   }
/*     */   
/*     */   private void runSql(Connection conn, String sql) {
/* 190 */     Matcher m = PAT_SELECT.matcher(sql);
/*     */     
/* 192 */     if (m.find()) {
/*     */       try
/*     */       {
/* 195 */         Statement stmt = conn.createStatement();
/* 196 */         ResultSet rs = stmt.executeQuery(sql);
/* 197 */         ResultSetMetaData md = rs.getMetaData();
/* 198 */         int colCount = md.getColumnCount();
/* 199 */         List<Object[]> firstRows = new ArrayList();
/* 200 */         int rowCount = 0;
/*     */         
/*     */ 
/* 203 */         int[] colWidths = new int[colCount];
/* 204 */         if (this.mShowColumnNames) {
/* 205 */           for (int i = 0; i < colCount; i++) {
/* 206 */             String name = md.getColumnLabel(i + 1);
/* 207 */             if (name.length() > colWidths[i]) {
/* 208 */               colWidths[i] = name.length();
/*     */             }
/*     */           }
/*     */         }
/*     */         
/*     */ 
/* 214 */         while ((rowCount < 1000) && (rs.next())) {
/* 215 */           Object[] row = getCurrentRow(rs);
/* 216 */           for (int i = 0; i < colCount; i++) {
/* 217 */             Object o = row[i];
/* 218 */             int width = o == null ? "NULL".length() : o.toString().length();
/* 219 */             if (width > colWidths[i]) {
/* 220 */               colWidths[i] = width;
/*     */             }
/*     */           }
/* 223 */           firstRows.add(row);
/* 224 */           rowCount++;
/*     */         }
/*     */         
/*     */ 
/* 228 */         if ((!this.mBatch) && (this.mShowColumnNames))
/*     */         {
/*     */ 
/* 231 */           printDivider(colWidths);
/*     */         }
/* 233 */         if (this.mShowColumnNames) {
/* 234 */           String[] colNames = new String[colCount];
/* 235 */           for (int i = 0; i < colCount; i++) {
/* 236 */             colNames[i] = md.getColumnLabel(i + 1);
/*     */           }
/* 238 */           printRow(colNames, colWidths);
/*     */         }
/* 240 */         if (!this.mBatch) {
/* 241 */           printDivider(colWidths);
/*     */         }
/* 243 */         for (Object[] row : firstRows) {
/* 244 */           printRow(row, colWidths);
/*     */         }
/*     */         
/*     */ 
/* 248 */         while (rs.next()) {
/* 249 */           Object[] row = getCurrentRow(rs);
/* 250 */           printRow(row, colWidths);
/*     */         }
/* 252 */         if (!this.mBatch) {
/* 253 */           printDivider(colWidths);
/*     */         }
/* 255 */         rs.close();
/* 256 */         stmt.close();
/*     */       } catch (SQLException e) {
/* 258 */         e.printStackTrace();
/* 259 */         System.err.println(e.getMessage());
/*     */       }
/*     */     } else {
/*     */       try
/*     */       {
/* 264 */         Statement stmt = conn.createStatement();
/* 265 */         int numRows = stmt.executeUpdate(sql);
/* 266 */         stmt.close();
/* 267 */         System.out.println("Updated " + numRows + " rows");
/*     */       } catch (SQLException e) {
/* 269 */         System.err.println(e.getMessage());
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void printDivider(int[] colWidths) {
/* 275 */     StringBuilder b = new StringBuilder();
/* 276 */     for (int i = 0; i < colWidths.length; i++) {
/* 277 */       b.append('+');
/* 278 */       for (int j = 0; j < colWidths[i] + 2; j++) {
/* 279 */         b.append('-');
/*     */       }
/*     */     }
/* 282 */     b.append('+');
/* 283 */     System.out.println(b);
/*     */   }
/*     */   
/*     */   private Object[] getCurrentRow(ResultSet rs) throws SQLException
/*     */   {
/* 288 */     ResultSetMetaData md = rs.getMetaData();
/* 289 */     int colCount = md.getColumnCount();
/* 290 */     Object[] row = new Object[colCount];
/* 291 */     for (int i = 0; i < colCount; i++) {
/* 292 */       row[i] = rs.getObject(i + 1);
/*     */     }
/* 294 */     return row;
/*     */   }
/*     */   
/*     */   private void printRow(Object[] row, int[] colWidths) {
/* 298 */     StringBuilder b = new StringBuilder();
/* 299 */     if (!this.mBatch) {
/* 300 */       b.append("| ");
/*     */     }
/* 302 */     String delimiter = this.mBatch ? "\t" : " | ";
/* 303 */     for (int i = 0; i < row.length; i++) {
/* 304 */       if (i > 0) {
/* 305 */         b.append(delimiter);
/*     */       }
/* 307 */       Object o = row[i];
/* 308 */       String s = o == null ? "NULL" : o.toString();
/* 309 */       b.append(s);
/* 310 */       if ((!this.mBatch) && 
/* 311 */         (s.length() < colWidths[i])) {
/* 312 */         for (int j = 0; j < colWidths[i] - s.length(); j++) {
/* 313 */           b.append(" ");
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 318 */     if (!this.mBatch) {
/* 319 */       b.append(" |");
/*     */     }
/* 321 */     System.out.println(b);
/*     */   }
/*     */   
/*     */   private void usage(ParseException e) {
/* 325 */     if (e != null) {
/* 326 */       System.err.println(e + "\n");
/*     */     }
/*     */     
/* 329 */     PrintWriter pw = new PrintWriter(System.err, true);
/* 330 */     HelpFormatter formatter = new HelpFormatter();
/* 331 */     formatter.printHelp(pw, formatter.getWidth(), "zmsql <options>", null, this.mOptions, formatter.getLeftPadding(), formatter.getDescPadding(), "");
/*     */   }
/*     */   
/*     */ 
/*     */   public static void main(String[] args)
/*     */   {
/* 337 */     JdbcClient app = new JdbcClient(args);
/* 338 */     app.run();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/db/JdbcClient.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */